#include "ScnsLcd.c.h"

#if defined(SCNS_LCD_ENABLE)&&SCNS_LCD_ENABLE==1

#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_ltdc.h"
#include "ScnsBspLcd.h"
#include "ltdc.h"
#include "ScnsSsd1306.h"

const ScnsBspLcdPhysicalConfig scnsBspLcdPhysicalConfig[SCNS_BSP_LCD_PHYSICAL_DEVICE_MAX]={
    [0]={.x=0,.y=0,.fullScreen=1,},
    [1]=scnsSsd1306LcdPhysicalConfig(LTDC_SCREEN_WIDTH,0),
};

Point scnsBspLcdGetBorder(uint8 pD)
{
    if(pD==0){return ((Point){.x=LTDC_SCREEN_WIDTH,.y=LTDC_SCREEN_HEIGHT});}
    else if(pD==1){return scnsSsd1306GetBorder(SCNS_SSD1306_1);}
    else{SCNS_ASSERT_ON_RUN(0);}
}

ScnsStatus scnsBspLcdSetBackgroundLight(ScnsLcdBackgroundLightStatus status)
{
    LCD_BK_GPIO_Port->BSRR=((status)==SCNS_LCD_BACKGROUND_LIGHT_ON)?(LCD_BK_Pin):(LCD_BK_Pin<<16);
    LTDC_WORK((status)==SCNS_LCD_BACKGROUND_LIGHT_ON);
    scnsSsd1306SetBackgroundLight(SCNS_SSD1306_1,status);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspLcdSetAddress(uint8 pd,Point plu,Point prd,uint8 write)
{
    if(pd==1){return scnsSsd1306SetAddress(SCNS_SSD1306_1,plu,prd,write);}
    return SCNS_STATUS_NOT_SUPPORT;;
}

ScnsStatus scnsBspLcdWriteColor(uint8 pd,void*buf,uint32 n,uint8 last,ScnsLock*lock)
{
    if(pd==1){return scnsSsd1306WriteColor(SCNS_SSD1306_1,buf,n,last,lock);}
    return SCNS_STATUS_NOT_SUPPORT;
}

ScnsStatus scnsBspLcdReadColor(uint8 pd,void*buf,uint32 n,uint8 last,ScnsLock*lock)
{
    if(pd==1){return scnsSsd1306ReadColor(SCNS_SSD1306_1,buf,n,last,lock);}
    return SCNS_STATUS_NOT_SUPPORT;
}

ScnsStatus scnsBspLcdPostInit(void)
{
    HAL_LTDC_SetAddress(&hltdc,(uint32)scnsLcdGetPhysicalAddress(0),LTDC_LAYER_1);
    return SCNS_STATUS_OK;
}

#endif